namespace TSL.Utility.Json
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Text;

    public class IListConverter : IJavaScriptConverter
    {
        public IListConverter()
        {
            base.m_AllowInheritance = true;
            base.m_serializableTypes = new Type[] { typeof(IList<>), typeof(IList), typeof(IEnumerable), typeof(string[]), typeof(int[]), typeof(bool[]) };
            base.m_deserializableTypes = base.m_serializableTypes;
        }

        public override object Deserialize(IJavaScriptObject o, Type t)
        {
            int num;
            JavaScriptArray array = o as JavaScriptArray;
            if (array == null)
            {
                throw new NotSupportedException();
            }
            if (t.IsArray)
            {
                Type elementType = Type.GetType(t.AssemblyQualifiedName.Replace("[]", ""));
                Array array2 = Array.CreateInstance(elementType, (array != null) ? array.Count : 0);
                try
                {
                    if (array == null)
                    {
                        return array2;
                    }
                    num = 0;
                    while (num < array.Count)
                    {
                        object obj2 = JavaScriptDeserializer.Deserialize(array[num], elementType);
                        array2.SetValue(obj2, num);
                        num++;
                    }
                }
                catch (InvalidCastException exception)
                {
                    throw new InvalidCastException("Array ('" + t.Name + "') could not be filled with value of type '" + elementType.Name + "'.", exception);
                }
                return array2;
            }
            if (!(typeof(IList).IsAssignableFrom(t) && (o is JavaScriptArray)))
            {
                throw new NotSupportedException();
            }
            IList list = (IList) Activator.CreateInstance(t);
            ParameterInfo info2 = t.GetMethod("Add").GetParameters()[0];
            for (num = 0; num < array.Count; num++)
            {
                list.Add(JavaScriptDeserializer.Deserialize(array[num], info2.ParameterType));
            }
            return list;
        }

        public override string Serialize(object o)
        {
            StringBuilder sb = new StringBuilder();
            this.Serialize(o, sb);
            return sb.ToString();
        }

        public override void Serialize(object o, StringBuilder sb)
        {
            IEnumerable enumerable = (IEnumerable) o;
            bool flag = true;
            sb.Append("[");
            foreach (object obj2 in enumerable)
            {
                if (!flag)
                {
                    sb.Append(",");
                }
                sb.Append(JavaScriptSerializer.Serialize(obj2));
                flag = false;
            }
            sb.Append("]");
        }

        public override bool TrySerializeValue(object o, Type t, StringBuilder sb)
        {
            if (t.IsArray)
            {
                this.Serialize(o, sb);
                return true;
            }
            return base.TrySerializeValue(o, t, sb);
        }
    }
}

